<?php

namespace app\data\controller\rework;

use app\data\service\ReworkService;
use think\admin\Controller;

/**
 * 品质问题
 * Class ReworkQuestion
 * @package app\data\controller\rework
 */
class ReworkQuestion extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'rework_question';

    /**
     * 品质问题管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = '品质问题管理';
        $query = $this->_query($this->table);
        $query->like('title');
        $query->order('id asc')->page();
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_page_filter(array &$data)
    {
        foreach ($data as &$item) {
            $item = ReworkService::instance()->format($item);
        }
    }

    /**
     * 添加品质问题
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑品质问题
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isGet()) {
            $data = ReworkService::instance()->format($data);
        } else {
            if (empty($data['is_answers'])) {
                $this->error('答案不能为空');
            }

            if (count($data['answers']) > 4) {
                $this->error('答案不能超过4个');
            }

            if (count($data['answers']) < 2) {
                $this->error('答案不能少与2个');
            }

            if(!$data['state'] && !empty($data['id'])) {
                if ($this->app->db->name($this->table)->where('state', 1)->where('id', '<>', $data['id'])->count() < 5) {
                    $this->error('至少保留5个启用的题目');
                }
            }

            $data = ReworkService::instance()->formatSaveQuestion($data);
        }
    }

    /**
     * 表单结果处理
     * @param boolean $state
     */
    protected function _form_result(bool $state)
    {
        if ($state) {
            $this->success('保存成功');
        }
    }

    /**
     * 删除品质问题
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $count = $this->app->db->name($this->table)->where('state', 1)->where('id', '<>', $this->request->request('id'))->count();
        if ($count < 5) {
            $this->error('至少保留5个启用的题目');
        }

        $this->_delete($this->table);
    }
}